﻿using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using Kingdee.BOS.WebApi.DataEntities;
using LanDBom.Models;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace LanDBom.Controllers
{
    public class LoginController : Controller
    {
        LDbaseEntities db = new LDbaseEntities();
        public ActionResult Loginiew()
        {
            List<Dengli> denglis = new List<Dengli>();
            return View(denglis);
        }
        //提交POst
        public ActionResult Loginie(string DEname, string DEpass, string DEquanxian)
        {
            if (DEquanxian == "ERP成员")
            {
                ///通过ERP系统的成员信息登录系统
                var client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //自定义接口需要先登录再调用
                string formId = "SEC_User";
                //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FName,FNumber";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式FPASSWORD
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    Limit = 200,
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());
                //返回的数据类型为List<List<object>>，错误的返回结果只有一行数据且IsSuccess为False
                if (result.Count == 1)
                {
                    //对返回结果进行解析和校验，这里使用的是JsonPatch
                    var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                    var queryNode = resultJObject.SelectToken("$..IsSuccess");
                    if (queryNode != null)
                    {
                        var isSuccess = queryNode.Value<Boolean>();
                        Info.AppendLine(isSuccess ? "操作成功" : "操作失败");
                    }
                    else
                    {
                        Info.AppendLine("操作成功");
                    }
                }
                else
                {
                    Info.AppendLine("操作成功");
                }
            }
            else
            {
                if (ModelState.IsValid)
                {
                    var denglis = db.Dengli.FirstOrDefault(s => s.DE_nae == DEname && s.DE_Pass == DEpass );
                    if (denglis == null)
                    {
                        return Content("1");
                    }
                    else
                    {
                        MyAuthentication.SerCankie(denglis.DE_nae, denglis.DE_ID.ToString(), "user");
                        return Content("0");
                    }
                }
            }
            return Content("1");
        }
        //添加员工
        public ActionResult Loginiewe(string DEname, string DEpass, string DEphone)
        {
            string mess = "";//输出
            try
            {
                var denglis = db.Dengli.FirstOrDefault(s => s.DE_nae == DEname && s.DE_phone == DEphone);
                if (denglis != null)
                {
                    mess = "该用户已存在";
                }
                else if (Regex.Matches(DEpass, "^(?=.*\\d)(?=.*[a-z])[a-z0-9]{6,12}$").Count < 0)
                {
                    mess = "密码输入不正确";
                }
                else
                {
                    Dengli dengli = new Dengli()
                    {
                        DE_quan = "员工",
                        DE_nae = DEname,
                        DE_Pass = DEpass,
                        DE_phone = DEphone
                    };
                    db.Dengli.Add(dengli);
                    int shun = db.SaveChanges();
                    mess = shun > 0 ? "注册成功" : "";
                    return Content(mess);
                }
            }
            catch (Exception ex)
            {
                mess = "错误信息：" + ex.Message;
            }
            return Content(mess);
        }
        /* public ActionResult Quanxian(string namees="")
         {

         }*/
    }
}
